package com.mall.controller;

import java.io.InputStream;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.mall.bean.User;
import com.mall.service.IPersonalService;

@Controller
public class PersonalController {

	@Resource
	private IPersonalService personalService;

	//展示个人中心
	@RequestMapping("person/personal")
	public String personal(User user, Model model) {
		System.out.println("user：" + user);
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);

		return "person/personal";
	}

	//更换头像
	@RequestMapping("changeImage")
	public String changeImage(@RequestParam("url") MultipartFile[] file, Model model, HttpSession session) throws Exception {

		User user = (User) session.getAttribute("user");
		List<String> arrary = new ArrayList<>();
		//生成不重复的随机set集合
		for (int i = 0; i < file.length; i++) {
			int n = 1;
			Set set = new HashSet();
			Random random = new Random();
			while (set.size() < n) {
				set.add(random.nextInt(100000000));
			}
			Iterator it = set.iterator();
			while (it.hasNext()) {
				Object number = it.next();
				System.out.println("开始循环咯");
				String url = file[i].getOriginalFilename();
				String type = file[i].getContentType().split("/")[1];
				String newurl = null;


				if ("jpeg".equals(type) || "jpg".equals(type) || "png".equals(type) || "bmp".equals(type) || "webq".equals(type) || "mp4".equals(type)) {
					newurl = "hdfs://iflytek002:9000/photo/" + user.getUsername() + "-" + number + "." + type;
					System.out.println("newurl：" + newurl);
					arrary.add(newurl);
					System.out.println("array：" + arrary.get(0));
					user.setHeadImage(arrary.get(0));
				}


			}
		}
		upload(file, arrary);
		personalService.updateImage(user);
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/personal";
	}

	//
	@RequestMapping("person/bill")
	public String bill(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/bill";

	}

	@RequestMapping("person/billlist")
	public String billlist(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/billlist";
	}

	@RequestMapping("person/bindphone")
	public String bindphone(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/bindphone";
	}

	@RequestMapping("person/bonus")
	public String bonus(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/bonus";
	}

	@RequestMapping("person/change")
	public String change(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/change";
	}

	@RequestMapping("person/collection")
	public String collection(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/collection";
	}

	@RequestMapping("person/comment")
	public String comment(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/comment";
	}

	@RequestMapping("person/coupon")
	public String coupon(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/coupon";
	}

	@RequestMapping("person/email")
	public String email(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/email";
	}

	@RequestMapping("person/foot")
	public String foot(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/foot";
	}

	@RequestMapping("person/idcard")
	public String idcard(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/idcard";
	}

	@RequestMapping("person/information")
	public String information(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/information";
	}

	@RequestMapping("person/logistics")
	public String logistics(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/logistics";
	}

	@RequestMapping("person/news")
	public String news(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/news";
	}

	@RequestMapping("person/orderinfo")
	public String orderinfo(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/orderinfo";
	}

	@RequestMapping("person/password")
	public String password(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/password";
	}

	@RequestMapping("person/question")
	public String question(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/question";
	}

	@RequestMapping("person/record")
	public String record(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/record";
	}

	@RequestMapping("person/refund")
	public String refund(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/refund";
	}

	@RequestMapping("person/safety")
	public String safety(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/safety";
	}

	@RequestMapping("person/setpay")
	public String setpay(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/setpay";
	}

	@RequestMapping("person/success")
	public String success(Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		User user1 = personalService.findPerMsg(user);
		model.addAttribute("user", user1);
		return "person/success";
	}

	//上传头像
	private void upload(@RequestParam("url") MultipartFile[] file, List<String> url) throws Exception {
		Configuration conf = new Configuration();
		// int length = file.length;
		for (int i = 0; i < file.length; i++) {
			InputStream in = file[i].getInputStream();
			URI hfdfsfile = new URI("hdfs://iflytek002:9000");
			conf.set("fs.defaultFS", "hdfs://iflytek002:9000");
			conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
			/* conf.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem"); */
			FileSystem fs = FileSystem.get(hfdfsfile, conf, "root");
			Path path = new Path(url.get(i));
			FSDataOutputStream out = fs.create(path);
			IOUtils.copyBytes(in, out, conf);
			fs.close();
			in.close();
		}
	}
}
